<template>
  <!-- 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/roles'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    // 获取角色信息
    this.loadRoles()

    // 根据id发送请求获取当前员工具备的角色信息
    this.loadRolesById()
  },
  methods: {
    // 获取角色信息
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 100 })
      // 保存数据
      this.list = res.data.rows
    },
    // 根据id发送请求获取当前员工具备的角色信息
    async loadRolesById() {
      const res = await getUserDetailById(this.curId)
      // console.log('getUserDetailById', res)
      // 数据回填
      this.roleIds = res.data.roleIds
    },

    // 点击确定，保存修改
    async hSubmit() {
      await assignRoles({ id: this.curId, roleIds: this.roleIds })
      // 提示
      this.$message.success('修改成功')
      // 关闭弹框
      this.$emit('close')
    },
    // 点击取消
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
